package com.smartwasser.yunhtgl.apphtgl.presenter;

import android.os.Handler;

import com.smartwasser.yunhtgl.apphtgl.bean.CaseCensor1Bean;
import com.smartwasser.yunhtgl.apphtgl.network.RetrofitApi;
import com.smartwasser.yunhtgl.apphtgl.network.Retrofits;
import com.smartwasser.yunhtgl.apphtgl.view.CaseCensorView;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CaseCensorPresenterImpl implements CaseCensorPresenter {
    private CaseCensorView caseCensorView;
    private Handler mHandler = new Handler();

    public CaseCensorPresenterImpl(CaseCensorView caseCensorView) {
        this.caseCensorView = caseCensorView;
    }

    @Override
    public void getMessages(String JSESSIONID, String id) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getCaseCensor(JSESSIONID, id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseCensor1Bean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                caseCensorView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final CaseCensor1Bean caseCensorBean) {
                if (caseCensorBean.getErrorCode().equals("00000")) {
                    caseCensorView.success(caseCensorBean);
                } else {
                    caseCensorView.error();
                }
            }
        });
    }

    @Override
    public void shMessage(String JSESSIONID, String ids, String flagF, String comment) {
        Retrofits.getInstance().createReq(RetrofitApi.class).shMessage(JSESSIONID, ids, flagF, comment).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                caseCensorView.errorSH();
                Logger.e("onError");
            }

            @Override
            public void onNext(Object object) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        caseCensorView.successSH();
                    }
                },1000);
            }
        });
    }
}